/*
 * Informacje.java
 *
 * Created on 2011-04-03, 11:15:07
 */
package medimg.widok.konto;

import java.util.Date;
import javax.swing.JOptionPane;
import medimg.scheme.Funkcje;
import medimg.scheme.Pacjenci;
import medimg.scheme.Personel;
import org.hibernate.JDBCException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author l0nger
 */
public class Informacje extends javax.swing.JDialog {

    /** Creates new form Informacje */
    public Informacje(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
        getCurrentUserInfo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField2 = new javax.swing.JTextField();
        jLabelImie = new javax.swing.JLabel();
        jLabelNazwisko = new javax.swing.JLabel();
        jLabelPesel = new javax.swing.JLabel();
        jLabelData = new javax.swing.JLabel();
        jLabelAdres = new javax.swing.JLabel();
        jLabelFunkcja = new javax.swing.JLabel();
        jTextFieldImie = new javax.swing.JTextField();
        jTextFieldNazwisko = new javax.swing.JTextField();
        jTextFieldPesel = new javax.swing.JTextField();
        jTextFieldData = new javax.swing.JTextField();
        jTextFieldFunkcja = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaAdres = new javax.swing.JTextArea();
        jButtonOk = new javax.swing.JButton();

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Informacje personalne");
        setModal(true);
        setName("Informacje"); // NOI18N
        setResizable(false);

        jLabelImie.setText("Imię");

        jLabelNazwisko.setText("Nazwisko");

        jLabelPesel.setText("PESEL");

        jLabelData.setText("Data urodzenia");

        jLabelAdres.setText("Adres");

        jLabelFunkcja.setText("Funkcja");

        jTextFieldImie.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldImie.setEnabled(false);

        jTextFieldNazwisko.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldNazwisko.setEnabled(false);

        jTextFieldPesel.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldPesel.setEnabled(false);

        jTextFieldData.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldData.setEnabled(false);

        jTextFieldFunkcja.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextFieldFunkcja.setEnabled(false);

        jTextAreaAdres.setColumns(20);
        jTextAreaAdres.setFont(new java.awt.Font("Tahoma", 0, 11));
        jTextAreaAdres.setRows(5);
        jTextAreaAdres.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        jTextAreaAdres.setEnabled(false);
        jTextAreaAdres.setOpaque(false);
        jScrollPane1.setViewportView(jTextAreaAdres);

        jButtonOk.setText("OK");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonOk, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelImie)
                            .addComponent(jLabelAdres)
                            .addComponent(jLabelPesel)
                            .addComponent(jLabelNazwisko)
                            .addComponent(jLabelData)
                            .addComponent(jLabelFunkcja))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldImie, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(jTextFieldNazwisko, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(jTextFieldPesel, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(jTextFieldData, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(jTextFieldFunkcja, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelImie)
                    .addComponent(jTextFieldImie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNazwisko, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNazwisko))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPesel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPesel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelData))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabelAdres))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldFunkcja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelFunkcja))
                .addGap(21, 21, 21)
                .addComponent(jButtonOk)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonOkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Informacje dialog = new Informacje(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOk;
    private javax.swing.JLabel jLabelAdres;
    private javax.swing.JLabel jLabelData;
    private javax.swing.JLabel jLabelFunkcja;
    private javax.swing.JLabel jLabelImie;
    private javax.swing.JLabel jLabelNazwisko;
    private javax.swing.JLabel jLabelPesel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaAdres;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextFieldData;
    private javax.swing.JTextField jTextFieldFunkcja;
    private javax.swing.JTextField jTextFieldImie;
    private javax.swing.JTextField jTextFieldNazwisko;
    private javax.swing.JTextField jTextFieldPesel;
    // End of variables declaration//GEN-END:variables


    public void getCurrentUserInfo(){
        if(medimg.hibernate.HibernateUtil.getSessionFactory()==null || !medimg.hibernate.HibernateUtil.isOpen()){
            JOptionPane.showConfirmDialog(this,"Najpierw podłącz się do bazy.","Nie polaczono",JOptionPane.DEFAULT_OPTION);
            return;
        }
        Session session = medimg.hibernate.HibernateUtil.getSessionFactory().getCurrentSession(); //pobranie obecnej sesji (autostart jesli jej nie ma)
        Pacjenci pacjent = null;
        Personel personel = null;
        Funkcje funkcja = null;

        try{
            session.beginTransaction(); //rozpoczecie transakcji
            personel = (Personel) session.createQuery("from Personel where pesel="+medimg.hibernate.HibernateUtil.getConfiguration().getProperty("hibernate.connection.username")).uniqueResult();
            if (personel != null) {
               funkcja = (Funkcje) session.createQuery("from Funkcje where id = (select funkcja from Personel where pesel = "+personel.getPesel()+")").uniqueResult(); 
            }
            session.getTransaction().commit();
        }
        catch(JDBCException ex){
             session.getTransaction().rollback(); //jesli sie nie udalo to rollback zeby dane byly zgodne
        }

        if(personel==null){
            session = medimg.hibernate.HibernateUtil.getSessionFactory().getCurrentSession(); //pobranie obecnej sesji (autostart jesli jej nie ma)
            try{
                session.beginTransaction(); //rozpoczecie transakcji
                pacjent = (Pacjenci) session.createQuery("from Pacjenci where pesel="+medimg.hibernate.HibernateUtil.getConfiguration().getProperty("hibernate.connection.username")).uniqueResult();
                session.getTransaction().commit();
            }
            catch(JDBCException ex){
                 session.getTransaction().rollback(); //jesli sie nie udalo to rollback zeby dane byly zgodne
            }
        }

        if(pacjent!=null){ //jesli znalazl pacjenta, ustawiamy pola
            setjTextFieldImie(pacjent.getImie());
            setjTextFieldNazwisko(pacjent.getNazwisko());
            setjTextFieldPesel(pacjent.getPesel());
            setjTextFieldData(pacjent.getDataUr());
            setjTextAreaAdres(pacjent.getAdres());
        }
        else{ //jesli nie to szukamy w pracownikach
            setjTextFieldImie(personel.getImie());
            setjTextFieldNazwisko(personel.getNazwisko());
            setjTextFieldPesel(personel.getPesel());
            setjTextFieldData(personel.getDataUr());
            setjTextAreaAdres(personel.getAdres());
            setjTextFieldFunkcja(funkcja.getNazwa());
        }
        return;
    }

    /**
     * @param java.lang.String the jTextAreaAdres to set
     */
    public void setjTextAreaAdres(java.lang.String newText) {
        jTextAreaAdres.setText(newText);
    }

    /**
     * @param java.lang.String the jTextFieldData to set
     */
    public void setjTextFieldData(Date newText) {
        jTextFieldData.setText(newText.toString());
    }

    /**
     * @param java.lang.String the jTextFieldFunkcja to set
     */
    public void setjTextFieldFunkcja(java.lang.String newText) {
        jTextFieldFunkcja.setText(newText);
    }

    /**
     * @param java.lang.String the jTextFieldImie to set
     */
    public void setjTextFieldImie(java.lang.String newText) {
        jTextFieldImie.setText(newText);
    }

    /**
     * @param java.lang.String the jTextFieldNazwisko to set
     */
    public void setjTextFieldNazwisko(java.lang.String newText) {
        jTextFieldNazwisko.setText(newText);
    }

    /**
     * @param java.lang.String the jTextFieldPesel to set
     */
    public void setjTextFieldPesel(java.lang.String newText) {
        jTextFieldPesel.setText(newText);
    }
}